<?php
/**
 * 搜窝
 *
 * @copyright 搜窝( www.souvo.com )
 * @package 前台用户登录
 * @version 1.0
 * @author wybwsk(wybwsk@qq.net)
 * @date 2011/5/2
 */
class MemberAction extends FrontAction{

    /**
     * 会员登出
     */
    public function logout() {
        $this->disAuthCookie();
        $this->redirect('main/index');
    }

    /**
     * 用户登陆操作
     */
    public function loginPost(){
        if($this->ajax) {
            $member = M('member');
            $username = $_POST['username'];
            $password = md5($_POST['password']);
            (empty($_POST['username']) || empty($_POST['password'])) && exit('3');
            if( $user = $member->where(array('username'=>$username))->find() ) {
                $user['password'] != $password && exit('2');
                $this->user = $user;
                $_SESSION['remeber'] == 'true';
                $expiry = $_POST['remember'] == 'true' ? 31536000 : 0;
                $this->setAuthCookie($expiry);
                M('member_info')->where(array('mid'=>$this->user['mid']))->data(array('last_login'=>time()))->save();
				cookie('redirect_url',null);
                exit('4');
            } else {
                exit('1');
            }
            exit('4');
        }
    }

    public function login() {
        if($this->user) {
            $this->redirect('main/index');
            exit();
        }
        if($this->ajax) {
            $this->assign('redirect_url',cookie('redirect_url') ? cookie('redirect_url') : U('memcp/index'));
            $this->assign('message',$message);
            $this->display();
        }else{
            $this->redirect('main/index');
        }
    }
    public function updatePass() {
        if($this->post) {
            (!$this->validTR(true) || !$this->post) && exit('非法');
            if($_POST['password'] != $_POST['password_again']) {
                $this->redirect('memcp/infoset','1','两次密码输入不正确');
                exit;
            }
            $strlen = mb_strlen($_POST['password'],'utf-8');
            if($strlen > 50 || $strlen < 6){
                alert('密码长度为6-50个字符','',2);
                exit();
            }
            $member=M('member')->field('password')->where(array('mid'=>$this->user['mid']))->find();
            if($member['password'] != md5($_POST['old_password'])){
                alert('原始密码不正确','memcp/infoset');
                exit;
            }else{
                $update_data['password'] = md5($_POST['password']);
                if(M('member')->data($update_data)->where(array('mid'=>$this->user['mid']))->save()) {
                    exit('密码修改成功');
                }else{
                    $this->halt('memcp/infoset','1','系统错误');
                    exit;
                }
            }
        }
    }

    /**
     * 用户注册
     */
    public function register() {
        if($this->ajax && $this->post) {
            $member = M('member');
            $username = $_POST['username'];
            $password = $_POST['password'];
            (empty($username) || empty($password)) && exit('6');
            $strlen = mb_strlen($username);
            ($strlen > 50 || $strlen < 6 ) && exit('7');
            $strlen = mb_strlen($password);
            ($strlen > 50 || $strlen < 6 ) && exit('8');
            $confirm_password = $_POST['confirm_password'];
            $email = $_POST['email'];
            $data = array(
                    'username' => $username,
                    'password' => md5($password),
                );
            md5($_POST['verify']) != $_SESSION['verify'] && exit('1');

            (!$this->validTR(true) || !$this->post) && exit('2');

            $password != $confirm_password && exit('3');

            !preg_match('/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/',$data['username']) && exit('4');

            $member->where(array('username'=>$data['username']))->count() && exit('5');

            if( $mid = $member->add($data) ) {
                M('member_info')->data(array('mid'=>$mid,'nickname'=>$data['username'],'update_time'=>time(),'reg_time'=>time()))->add();
                $this->user = M('member')->find($mid);
                $expiry = $_POST['remember'] == 'true' ? 31536000 : 0;
                $this->setAuthCookie($expiry);
                cookie('redirect_url',NULL);
                exit('9');
            } else {
                exit('10');
            }
            exit;
        }
        $this->assign('redirect_url',cookie('redirect_url') ? cookie('redirect_url') : U('main/index'));
        $this->display();
    }

    /**
     * 验证码
     */
    public function verify() {
        import("ORG.Util.Image");
        Image::buildImageVerify($length=5,$mode=1,$type='png',$width=68,$height=27,$verifyName='verify');
    }

    public function _empty() {
        $this->login();
    }
}
?>
